package cn.lg.soar.common.data;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 数据转换工具
 * @author luguoxiang 469599794@qq.com
 * @Date: 2025/4/15 17:33
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public abstract class ConversionUtils {

    protected static final DateTimeFormatter yyyyMMddHHmmssSSS = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    protected static final DateTimeFormatter yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    protected static final DateTimeFormatter yyyyMMddHHmm = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
    protected static final DateTimeFormatter yyyyMMddHH = DateTimeFormatter.ofPattern("yyyyMMddHH");
    protected static final DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
    protected static final DateTimeFormatter HHmmssSSS = DateTimeFormatter.ofPattern("HHmmssSSS");
    protected static final DateTimeFormatter HHmmss = DateTimeFormatter.ofPattern("HHmmss");
    protected static final DateTimeFormatter HHmm = DateTimeFormatter.ofPattern("HHmm");
    protected static final DateTimeFormatter HH = DateTimeFormatter.ofPattern("HH");
    protected static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    protected static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    protected static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");

    protected static final Pattern GET_NUMBER_PATTERN = Pattern.compile("[^0-9]");
    protected static final  ZoneId ZONE_ID = ZoneId.systemDefault();

    protected static final Set<String> BOOLEAN_TRUE = toSet("true", "yes", "y", "1", "是", "对", "正确");
    protected static final Set<String> BOOLEAN_FALSE = toSet("false", "no", "n", "0", "否", "不是", "错", "不对", "不正确");

    protected static String getNumber(String str) {
        return GET_NUMBER_PATTERN.matcher(str).replaceAll("");
    }

    @SafeVarargs
    public static <T>Set<T> toSet(T... elements) {
        if (elements == null) {
            return null;
        }
        Set<T> resultSet = new HashSet<>(elements.length);
        Collections.addAll(resultSet, elements);
        return resultSet;
    }
    public static <T>Set<T> toSet(Collection<T> collection) {
        if (collection == null) {
            return null;
        }
        return new HashSet<>(collection);
    }

    @SafeVarargs
    public static <T>List<T> toList(T... elements) {
        if (elements == null) {
            return null;
        }
        List<T> resultSet = new ArrayList<>(elements.length);
        Collections.addAll(resultSet, elements);
        return resultSet;
    }
    public static <T>List<T> toList(Collection<T> collection) {
        if (collection == null) {
            return null;
        }
        return new ArrayList<>(collection);
    }

    public static BigDecimal toBigDecimal(String value) {
        if (value == null) {
            return null;
        }
        return new BigDecimal(value);
    }
    public static BigDecimal toBigDecimal(Number value) {
        if (value == null) {
            return null;
        }
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        if (value instanceof Long) {
            return BigDecimal.valueOf(value.longValue());
        }
        if (value instanceof BigInteger) {
            return new BigDecimal((BigInteger) value);
        }
        return BigDecimal.valueOf(value.doubleValue());
    }
    public static BigDecimal toBigDecimal(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return toBigDecimal((Number) value);
        }
        return  new BigDecimal(value.toString());
    }

    public static BigInteger toBigInteger(String value) {
        if (value == null) {
            return null;
        }
        try {
            return new BigInteger(value);
        } catch (RuntimeException e) {
            BigDecimal bigDecimal = new BigDecimal(value);
            if (bigDecimal.stripTrailingZeros().scale() <= 0) {
                return bigDecimal.toBigInteger();
            }
            throw e;
        }
    }
    public static BigInteger toBigInteger(Number value) {
        if (value == null) {
            return null;
        }
        if (value instanceof BigInteger) {
            return (BigInteger) value;
        }
        if (value instanceof BigDecimal) {
            BigDecimal bigDecimal = (BigDecimal) value;
            if (bigDecimal.stripTrailingZeros().scale() <= 0) {
                return bigDecimal.toBigInteger();
            }
        } else {
            long l = value.longValue();
            if (l == value.doubleValue()) {
                return BigInteger.valueOf(l);
            }
        }
        throw new NumberFormatException("Can not convert " + value + " to BigInteger");
    }
    public static BigInteger toBigInteger(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return toBigInteger((Number) value);
        }
        return toBigInteger(value.toString());
    }

    public static Long toLong(String value, DecimalFormat format) {
        if (value == null) {
            return null;
        }
        try {
            Number v = format.parse(value);
            long l = v.longValue();
            if (l == v.doubleValue()) {
                return l;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        throw new NumberFormatException("Can not convert " + value + " to Long");
    }
    public static Long toLong(String value, String format) {
        return toLong(value, new DecimalFormat(format));
    }
    public static Long toLong(String value) {
        if (value == null) {
            return null;
        }
        try {
            return Long.parseLong(value);
        } catch (RuntimeException e) {
            BigDecimal bigDecimal = new BigDecimal(value);
            long l = bigDecimal.longValue();
            if (l == bigDecimal.doubleValue()) {
                return l;
            }
            throw new NumberFormatException("Can not convert " + value + " to Long");
        }
    }
    public static Long toLong(Number value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Long) {
            return (Long) value;
        }
        long l = value.longValue();
        if (l == value.doubleValue()) {
            return l;
        }
        throw new NumberFormatException("Can not convert " + value + " to Long");
    }
    public static Long toLong(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return toLong((Number) value);
        }
        return toLong(value.toString());
    }
    public static Long toLong(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        switch (bytes.length) {
            case 8: return ((bytes[ 0 ] & 0xffL) << 56)
                    | ((bytes[ 1 ] & 0xffL) << 48)
                    | ((bytes[ 2 ] & 0xffL) << 40)
                    | ((bytes[ 3 ] & 0xffL) << 32)
                    | ((bytes[ 4 ] & 0xffL) << 24)
                    | ((bytes[ 5 ] & 0xffL) << 16)
                    | ((bytes[ 6 ] & 0xffL) << 8)
                    | (bytes[ 7 ] & 0xffL);
            case 7: return ((bytes[ 0 ] & 0xffL) << 48)
                    | ((bytes[ 1 ] & 0xffL) << 40)
                    | ((bytes[ 2 ] & 0xffL) << 32)
                    | ((bytes[ 3 ] & 0xffL) << 24)
                    | ((bytes[ 4 ] & 0xffL) << 16)
                    | ((bytes[ 5 ] & 0xffL) << 8)
                    | (bytes[ 6 ] & 0xffL);
            case 6: return ((bytes[ 0 ] & 0xffL) << 40)
                    | ((bytes[ 1 ] & 0xffL) << 32)
                    | ((bytes[ 2 ] & 0xffL) << 24)
                    | ((bytes[ 3 ] & 0xffL) << 16)
                    | ((bytes[ 4 ] & 0xffL) << 8)
                    | (bytes[ 5 ] & 0xffL);
            case 5: return ((bytes[ 0 ] & 0xffL) << 32)
                    | ((bytes[ 1 ] & 0xffL) << 24)
                    | ((bytes[ 2 ] & 0xffL) << 16)
                    | ((bytes[ 3 ] & 0xffL) << 8)
                    | (bytes[ 4 ] & 0xffL);
            case 4: return ((bytes[ 0 ] & 0xffL) << 24)
                    | ((bytes[ 1 ] & 0xffL) << 16)
                    | ((bytes[ 2 ] & 0xffL) << 8)
                    | (bytes[ 3 ] & 0xffL);
            case 3: return ((bytes[ 0 ] & 0xffL) << 16)
                    | ((bytes[ 1 ] & 0xffL) << 8)
                    | (bytes[ 2 ] & 0xffL);
            case 2: return ((bytes[ 0 ] & 0xffL) << 8)
                    | (bytes[ 1 ] & 0xffL);
            case 1: return (bytes[ 0 ] & 0xffL);
            default: throw new NumberFormatException("Can not convert to Long：Precision overflow");
        }
    }
    public static Long toLong(LocalDateTime value) {
        if (value == null) {
            return null;
        }
        return value.atZone(ZONE_ID).toInstant().toEpochMilli();
    }
    public static Long toLong(LocalDate value) {
        if (value == null) {
            return null;
        }
        return value.atStartOfDay(ZONE_ID).toInstant().toEpochMilli();
    }
    public static Long toLong(LocalTime value) {
        if (value == null) {
            return null;
        }
        return LocalDateTime.of(LocalDate.now(), value).atZone(ZONE_ID).toInstant().toEpochMilli();
    }
    public static Long toLong(Date value) {
        if (value == null) {
            return null;
        }
        return value.getTime();
    }

    public static Integer toInteger(String value, DecimalFormat format) {
        if (value == null) {
            return null;
        }
        try {
            Number v = format.parse(value);
            int l = v.intValue();
            if (l == v.doubleValue()) {
                return l;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        throw new NumberFormatException("Can not convert " + value + " to Integer");
    }
    public static Integer toInteger(String value, String format) {
        return toInteger(value, new DecimalFormat(format));
    }
    public static Integer toInteger(String value) {
        if (value == null) {
            return null;
        }
        try {
            return Integer.parseInt(value);
        } catch (RuntimeException e) {
            Double v = Double.parseDouble(value);
            int i = v.intValue();
            if (i == v) {
                return i;
            }
            throw new NumberFormatException("Can not convert " + value + " to Integer");
        }
    }
    public static Integer toInteger(Number value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Integer) {
            return (Integer) value;
        }
        int v = value.intValue();
        if (v == value.doubleValue()) {
            return v;
        }
        throw new NumberFormatException("Can not convert " + value + " to Integer");
    }
    public static Integer toInteger(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return toInteger((Number) value);
        }
        return toInteger(value.toString());
    }
    public static Integer toInteger(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        switch (bytes.length) {
            case 4: return ((bytes[ 0 ] & 0xff) << 24)
                    | ((bytes[ 1 ] & 0xff) << 16)
                    | ((bytes[ 2 ] & 0xff) << 8)
                    | (bytes[ 3 ] & 0xff);
            case 3: return ((bytes[ 0 ] & 0xff) << 16)
                    | ((bytes[ 1 ] & 0xff) << 8)
                    | (bytes[ 2 ] & 0xff);
            case 2: return ((bytes[ 0 ] & 0xff) << 8)
                    | (bytes[ 1 ] & 0xff);
            case 1: return (bytes[ 0 ] & 0xff);
            default: throw new NumberFormatException("Can not convert to Integer：Precision overflow");
        }
    }

    public static Double toDouble(String value, DecimalFormat format) {
        if (value == null) {
            return null;
        }
        try {
            Number v = format.parse(value);
            return v.doubleValue();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static Double toDouble(String value, String format) {
        return toDouble(value, new DecimalFormat(format));
    }
    public static Double toDouble(String value) {
        if (value == null) {
            return null;
        }
        return Double.parseDouble(value);
    }
    public static Double toDouble(Number value) {
        if (value == null) {
            return null;
        }
        return value.doubleValue();
    }
    public static Double toDouble(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return ((Number)value).doubleValue();
        }
        return toDouble(value.toString());
    }
    public static Double toDouble(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        if (bytes.length == 8) {
            return ByteBuffer.wrap(bytes).getDouble();
        }
        throw new NumberFormatException("Can not convert to Double, Length must be 8");
    }

    public static Float toFloat(String value, DecimalFormat format) {
        if (value == null) {
            return null;
        }
        try {
            Number v = format.parse(value);
            float l = v.floatValue();
            if (l == v.doubleValue()) {
                return l;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        throw new NumberFormatException("Can not convert " + value + " to Float");
    }
    public static Float toFloat(String value, String format) {
        return toFloat(value, new DecimalFormat(format));
    }
    public static Float toFloat(String value) {
        if (value == null) {
            return null;
        }
        return Float.parseFloat(value);
    }
    public static Float toFloat(Number value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Float) {
            return (Float) value;
        }
        double v = value.doubleValue();
        float f = value.floatValue();
        if (v == f) {
            return f;
        }
        throw new NumberFormatException("Can not convert " + value + " to Float");
    }
    public static Float toFloat(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return toFloat((Number) value);
        }
        return toFloat(value.toString());
    }
    public static Float toFloat(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        if (bytes.length == 4) {
            return ByteBuffer.wrap(bytes).getFloat();
        }
        throw new NumberFormatException("Can not convert to Float, Length must be 4");
    }

    public static Boolean toBoolean(String value) {
        if (value == null) {
            return null;
        }
        if (BOOLEAN_TRUE.contains(value)) {
            return true;
        }
        if (BOOLEAN_FALSE.contains(value)) {
            return false;
        }
        return Boolean.valueOf(value);
    }
    public static Boolean toBoolean(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Boolean) {
            return (Boolean) value;
        }
        return Boolean.valueOf(value.toString());
    }

    public static LocalDateTime toLocalDateTime(Long millisecond) {
        if (millisecond == null) {
            return null;
        }
        Instant instant = Instant.ofEpochMilli(millisecond);
        return LocalDateTime.ofInstant(instant, ZONE_ID);
    }
    public static LocalDateTime toLocalDateTime(Number value, ChronoUnit unit) {
        if (value == null) {
            return null;
        }
        double time = value.doubleValue();
        switch (unit) {
            case MILLENNIA: break;
            case SECONDS: time = time * 1000; break;
            case MINUTES: time = time * 60_000; break;
            case HOURS: time = time * 3600_000; break;
            case DAYS: time = time * 86400_000; break;
            default: throw new RuntimeException("不支持");
        }
        Instant instant = Instant.ofEpochMilli((long) time);
        return LocalDateTime.ofInstant(instant, ZONE_ID);
    }
    public static LocalDateTime toLocalDateTime(String value, DateTimeFormatter formatter) {
        if (value == null) {
            return null;
        }
        return LocalDateTime.parse(value, formatter);
    }
    public static LocalDateTime toLocalDateTime(String value, String format) {
        return toLocalDateTime(value, DateTimeFormatter.ofPattern(format));
    }
    public static LocalDateTime toLocalDateTime(String value) {
        if (value == null) {
            return null;
        }
        String str = getNumber(value);
        switch (str.length()) {
            case 17: return LocalDateTime.parse(str, yyyyMMddHHmmssSSS);
            case 14: return LocalDateTime.parse(str, yyyyMMddHHmmss);
            case 12: return LocalDateTime.parse(str, yyyyMMddHHmm);
            case 10: return LocalDateTime.parse(str, yyyyMMddHH);
            case 8: return LocalDateTime.of(LocalDate.parse(str, yyyyMMdd), LocalTime.MIN);
            case 6: return LocalDateTime.of(LocalDate.parse(str + "01", yyyyMMdd), LocalTime.MIN);
            case 4: return LocalDateTime.of(Integer.parseInt(str), 1, 1, 0, 0, 0);
            default: return toLocalDateTime(Long.parseLong(str));
        }
    }
    public static LocalDateTime toLocalDateTime(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof LocalDateTime) {
            return (LocalDateTime) value;
        }
        if (value instanceof LocalDate) {
            return LocalDateTime.of((LocalDate) value, LocalTime.MIN);
        }
        if (value instanceof LocalTime) {
            return LocalDateTime.of(LocalDate.now(), (LocalTime) value);
        }
        if (value instanceof Number) {
            return toLocalDateTime((Long) value);
        }
        return toLocalDateTime(value.toString());
    }

    public static LocalDate toLocalDate(Long millisecond) {
        if (millisecond == null) {
            return null;
        }
        Instant instant = Instant.ofEpochMilli(millisecond);
        return LocalDateTime.ofInstant(instant, ZONE_ID).toLocalDate();
    }
    public static LocalDate toLocalDate(Number millisecond, ChronoUnit unit) {
        if (millisecond == null) {
            return null;
        }
        double time = millisecond.doubleValue();
        switch (unit) {
            case MILLENNIA: time = time / 86400_000; break;
            case SECONDS: time = time / 86400; break;
            case MINUTES: time = time / 1440; break;
            case HOURS: time = time / 24; break;
            case DAYS: break;
            default: throw new RuntimeException("不支持");
        }
        return LocalDate.ofEpochDay((long) time);
    }
    public static LocalDate toLocalDate(String value, DateTimeFormatter formatter) {
        if (value == null) {
            return null;
        }
        return LocalDate.parse(value, formatter);
    }
    public static LocalDate toLocalDate(String value, String format) {
        return toLocalDate(value, DateTimeFormatter.ofPattern(format));
    }
    public static LocalDate toLocalDate(String value) {
        if (value == null) {
            return null;
        }
        String str = getNumber(value);
        int length = str.length();
        switch (length) {
            case 17:
            case 14:
            case 12:
            case 10: return LocalDate.parse(str.substring(0, 8), yyyyMMdd);
            case 8: return LocalDate.parse(str, yyyyMMdd);
            case 6: return LocalDate.parse(str + "01", yyyyMMdd);
            case 4: return LocalDate.of(Integer.parseInt(str), 1, 1);
            default: return toLocalDate(Long.parseLong(str));
        }
    }
    public static LocalDate toLocalDate(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof LocalDate) {
            return (LocalDate) value;
        }
        if (value instanceof Number) {
            return toLocalDate((Long) value);
        }
        return toLocalDate(value.toString());
    }

    public static LocalTime toLocalTime(Long millisecond) {
        if (millisecond == null) {
            return null;
        }
        Instant instant = Instant.ofEpochMilli(millisecond);
        return LocalDateTime.ofInstant(instant, ZONE_ID).toLocalTime();
    }
    public static LocalTime toLocalTime(String value, DateTimeFormatter formatter) {
        if (value == null) {
            return null;
        }
        return LocalTime.parse(value, formatter);
    }
    public static LocalTime toLocalTime(String value, String format) {
        return toLocalTime(value, DateTimeFormatter.ofPattern(format));
    }
    public static LocalTime toLocalTime(String value) {
        if (value == null) {
            return null;
        }
        String str = getNumber(value);
        switch (str.length()) {
            case 17: return LocalTime.parse(str.substring(8), HHmmssSSS);
            case 14: return LocalTime.parse(str.substring(8), HHmmss);
            case 12: return LocalTime.parse(str.substring(8), HHmm);
            case 10: return LocalTime.parse(str.substring(8), HH);
            case 9: return LocalTime.parse(str, HHmmssSSS);
            case 6: return LocalTime.parse(str, HHmmss);
            case 4: return LocalTime.parse(str, HHmm);
            case 2: return LocalTime.parse(str, HH);
            default: return toLocalTime(Long.parseLong(str));
        }
    }
    public static LocalTime toLocalTime(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof LocalTime) {
            return (LocalTime) value;
        }
        if (value instanceof Number) {
            return toLocalTime((Long) value);
        }
        return toLocalTime(value.toString());
    }

    public static Date toDate(Long millisecond) {
        if (millisecond == null) {
            return null;
        }
        return new Date(millisecond);
    }
    public static Date toDate(Number value, ChronoUnit unit) {
        if (value == null) {
            return null;
        }
        double time = value.doubleValue();
        switch (unit) {
            case MILLENNIA: break;
            case SECONDS: time = time * 1000; break;
            case MINUTES: time = time * 60_000; break;
            case HOURS: time = time * 3600_000; break;
            case DAYS: time = time * 86400_000; break;
            default: throw new RuntimeException("不支持");
        }
        return new Date((long) time);
    }
    public static Date toDate(String value, String format) {
        if (value == null) {
            return null;
        }
        try {
            return new SimpleDateFormat(format).parse(value);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static Date toDate(String value) {
        if (value == null) {
            return null;
        }
        value = getNumber(value);
        try {
            switch (value.length()) {
                case 4: return new SimpleDateFormat("yyyy").parse(value);
                case 6: return new SimpleDateFormat("yyyyMM").parse(value);
                case 8: return new SimpleDateFormat("yyyyMMdd").parse(value);
                case 10: return new SimpleDateFormat("yyyyMMddHH").parse(value);
                case 12: return new SimpleDateFormat("yyyyMMddHHmm").parse(value);
                case 14: return new SimpleDateFormat("yyyyMMddHHmmss").parse(value);
                case 17: return new SimpleDateFormat("yyyyMMddHHmmssSSS").parse(value);
                default: return new Date(Long.parseLong(value));
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    public static Date toDate(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Date) {
            return (Date) value;
        }
        if (value instanceof Number) {
            return toDate((Long) value);
        }
        return toDate(value.toString());
    }

    public static String toString(Number value, DecimalFormat format) {
        if (value == null) {
            return null;
        }
        return format.format(value);
    }
    public static String toString(Number value, String format) {
        if (value == null) {
            return null;
        }
        return new DecimalFormat(format).format(value);
    }
    public static String toString(Number value) {
        if (value == null) {
            return null;
        }
        return value.toString();
    }
    public static String toString(LocalDateTime value, DateTimeFormatter formatter) {
        if (value == null) {
            return null;
        }
        return value.format(formatter);
    }
    public static String toString(LocalDateTime value, String format) {
        return toString(value, DateTimeFormatter.ofPattern(format));
    }
    public static String toString(LocalDateTime value) {
        return toString(value, DATE_TIME_FORMATTER);
    }
    public static String toString(LocalDate value, DateTimeFormatter formatter) {
        if (value == null) {
            return null;
        }
        return value.format(formatter);
    }
    public static String toString(LocalDate value, String format) {
        return toString(value, DateTimeFormatter.ofPattern(format));
    }
    public static String toString(LocalDate value) {
        return toString(value, DATE_FORMATTER);
    }
    public static String toString(LocalTime value, DateTimeFormatter formatter) {
        if (value == null) {
            return null;
        }
        return value.format(formatter);
    }
    public static String toString(LocalTime value, String format) {
        return toString(value, DateTimeFormatter.ofPattern(format));
    }
    public static String toString(LocalTime value) {
        return toString(value, TIME_FORMATTER);
    }
    public static String toString(Date value, String format) {
        return new SimpleDateFormat(format).format(value);
    }
    public static String toString(Date value) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
    }
    public static String toString(Object value) {
        if (value == null) {
            return null;
        }
        return value.toString();
    }

    public static byte[] toBytes(Long value) {
        if (value == null) {
            return null;
        }
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(value);
        byte[] bytes = buffer.array();
        // 找到第一个非零字节的位置
        int startIndex = 0;
        while (startIndex < bytes.length && bytes[startIndex] == 0) {
            startIndex++;
        }
        // 如果所有字节都是 0，则返回一个长度为 1 的数组，包含单个 0
        if (startIndex == bytes.length) {
            return new byte[]{0};
        }
        // 创建新的数组来保存有效的字节
        byte[] result = new byte[bytes.length - startIndex];
        System.arraycopy(bytes, startIndex, result, 0, bytes.length - startIndex);
        return result;
    }
    public static byte[] toBytes(Integer value) {
        if (value == null) {
            return null;
        }
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(value);
        byte[] bytes = buffer.array();
        // 找到第一个非零字节的位置
        int startIndex = 0;
        while (startIndex < bytes.length && bytes[startIndex] == 0) {
            startIndex++;
        }
        // 如果所有字节都是 0，则返回一个长度为 1 的数组，包含单个 0
        if (startIndex == bytes.length) {
            return new byte[]{0};
        }
        // 创建新的数组来保存有效的字节
        byte[] result = new byte[bytes.length - startIndex];
        System.arraycopy(bytes, startIndex, result, 0, bytes.length - startIndex);
        return result;
    }
    public static byte[] toBytes(Double value) {
        if (value == null) {
            return null;
        }
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putDouble(value);
        return buffer.array();
    }
    public static byte[] toBytes(Float value) {
        if (value == null) {
            return null;
        }
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putFloat(value);
        return buffer.array();
    }

}
